package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.ManagerDao;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.RbacManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName RbacManagerServiceImpl
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/8/22 9:48
 * @Version 1.0
 */
@Service
public class RbacManagerServiceImpl implements RbacManagerService{

    @Autowired
    private ManagerDao managerDao;

    public ManagerDao getManagerDao() {
        return managerDao;
    }

    public void setManagerDao(ManagerDao managerDao) {
        this.managerDao = managerDao;
    }

    @Override
    public ResponseEntity selectAll(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list= managerDao.selectAll();
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "成功", pageInfo);
    }

    @Override
    @Transactional
    public int addUser(User user) {
        int row = managerDao.addUser(user);
        return row;
    }

    @Override
    public void updateUser(User user) {
        managerDao.updateUser(user);
    }

    @Override
    public int deleteUserById(int id) {
        int rows = managerDao.deleteUserById(id);
        return rows;
    }

    @Override
    public List<User> findUserByName(String username) {
        return managerDao.findUserByName(username);
    }

    @Override
    public ResponseEntity editOne(User user) {
        if (user.getId()!=0){
            int count = managerDao.findCountByNameOrId(user);
            if(count>1){
                return new ResponseEntity(500,"修改失败,账号同名！",null);
            }else{
                int i = managerDao.updateUser(user);
                if (i>0){
                    return new ResponseEntity(200,"成功修改",null);
                }else {
                    return new ResponseEntity(500,"修改失败",null);
                }
            }
        }else{
            return new ResponseEntity(500,"id不能没有！",null);
        }
    }

    @Override
    public ResponseEntity search(int currentPage, int pageSize,String account) {
        PageHelper.startPage(currentPage,pageSize);
        List<User> list= managerDao.search(account);
        PageInfo pageInfo = new PageInfo(list);
        return new ResponseEntity(200, "", pageInfo);
    }

    @Override
    public User findOneByName(String username) {
        return managerDao.findOneByName(username);
    }
}
